﻿using System;
using System.Text;
using System.Windows;
using Microsoft.Phone.Controls;
using PhoneNetTools.Dns;

namespace Sample.Dns
{
    public partial class MainPage : PhoneApplicationPage
    {
        private DnsHelper _dnsHelper;
        
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(HostNameTextBox.Text))
            {
                MessageBox.Show("Please enter a host name to check.");
                return;
            }

            // simply kick off a request to one of the entries 
            // from the name server list maintained in the DnsResolver class.
            // if you want to use alternate name servers or provide your own list,
            // you can do this by using the overload that takes the name servers.
            // By default, the DnsResolver name server list contains the two 
            // DNS servers from the OpenDNS project.
            _dnsHelper = new DnsHelper();
            _dnsHelper.BeginGetHostEntry(HostNameTextBox.Text, GetHostEntry_Callback, null);
        }

        private void GetHostEntry_Callback(IAsyncResult ar)
        {
            // this comes from a background thread.
            // you need to marshal it back to the UI thread if you want to access UI elements (also when you use MessageBox like below),
            // or synchronize access to shared resources to avoid threading issues.
            if (!CheckAccess())
            {
                Dispatcher.BeginInvoke(() => GetHostEntry_Callback(ar));
                return;
            }

            try
            {
                // simply call the "EndXYZ" method, passing on the async result,
                // to get the results. If there are pending exceptions, calling this
                // method will throw them
                var addresses = _dnsHelper.EndGetHostEntry(ar);

                // in our case, we simply concatenate the returned addresses (there
                // are potentially more than one) and show them.
                if (addresses != null)
                {
                    var sb = new StringBuilder();
                    foreach (var address in addresses)
                    {
                        sb.AppendLine(address.ToString());
                    }

                    MessageBox.Show("The obtained IP address(es): " + Environment.NewLine + sb);
                }
                else
                {
                    MessageBox.Show("Unfortunately, the server did not return any addresses.");
                }
            }
            catch (Exception ex)
            {
                // you can test for more specific exceptions here,
                // for example if a server error was returned, then the exception thrown
                // will be of type DnsException and contain the response code from the server
                // with more details.
                MessageBox.Show("An error occurred: " + ex.Message);
            }
        }
    }
}